package com.fzu.ess.ess.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fzu.ess.common.Const;
import com.fzu.ess.common.Model.JsonModel;
import com.fzu.ess.ess.entity.Book;
import com.fzu.ess.ess.entity.Language;
import com.fzu.ess.ess.entity.Profile;
import com.fzu.ess.ess.service.BookService;
import com.fzu.ess.ess.service.LanguageService;
import com.fzu.ess.ess.service.ProfileService;
import com.fzu.ess.sys.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ercha on 2015/10/25.
 */
@Controller
@RequestMapping(value = "/book")
public class BookController{
    @Autowired
    private BookService bookService;

    @Autowired
    private LanguageService languageService;

    @Autowired
    private HttpSession session;

    @Autowired
    private ProfileService profileService;

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String createPage(Model model){
        model.addAttribute("languageList", languageService.getAll());
        return "/jsp/user/book/create.jsp";
    }

    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public JsonModel create(Book book, String language, String images){
        System.out.println("here create book");
        System.out.println("images: " + images);
        JsonModel jsonModel = new JsonModel();
        List<String> imageList = new ArrayList<>();
        try {
            if(images!=null && images.length()>1){
                imageList = JSON.parseObject(images, new TypeReference<List<String>>() {
                });
            }


            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            User user = (User)session.getAttribute(Const.SESSION.USER);

            book.setOwner(user);
            book.setBookLanguage(new Language(language));
            book.setImageList(imageList);

            jsonModel.setSuccess(null != bookService.create(book));
        } catch(Exception e){
            e.printStackTrace();
        }

        return jsonModel;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String listPage(Model model){
        model.addAttribute("languageList", languageService.getAll());
        User user = (User)session.getAttribute(Const.SESSION.USER);

        int level = user.getLevel();
        switch (level){
            case Const.USER_LEVEL.ADMIN :
            case Const.USER_LEVEL.SUPER_ADMIN :
                return "/jsp/admin/book/list.jsp";

            case Const.USER_LEVEL.USER :
                return "/jsp/user/book/list.jsp";

            default: return "redirect:/";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/pageData", method = RequestMethod.GET)
    public JsonModel getPageData(@RequestParam(value = "page") Integer pageNumber, @RequestParam Integer pageSize,
                                 @RequestParam(required = false) String filter){
        return bookService.getDataByPage(pageNumber, pageSize, filter);
    }


    @RequestMapping(value = "/display", method = RequestMethod.GET)
    public String displayPage(@RequestParam String id, Model model){
        Book book = bookService.getById(id);
        Profile profile = profileService.getByUserId(book.getOwner().getId());
        model.addAttribute("book", book);
        model.addAttribute("profile", profile);
        return "/jsp/common/book/displayBook.jsp";
    }

    @ResponseBody
    @RequestMapping(value = "/toggleStatus", method = RequestMethod.POST)
    public JsonModel toggleStatus(String bookId, String userId, Integer currentStatus){
        return bookService.toggleStatus(bookId, userId, currentStatus);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detailPage(@RequestParam String id, Model model){
        model.addAttribute("book", bookService.getById(id));
        return "/jsp/common/book/detailBook.jsp";
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String updatePage(@RequestParam String id, Model model){
        model.addAttribute("book", bookService.getById(id));
        model.addAttribute("languageList", languageService.getAll());
        return "/jsp/user/book/update.jsp";
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonModel update(Book book, String language, String images){
        return bookService.update(book, language, images);
    }

    @ResponseBody
    @RequestMapping(value = "/deleteByUser", method = RequestMethod.POST)
    public JsonModel deleteByUser(String bookId, String userId){
        return bookService.deleteByUser(bookId, userId);
    }

    /*****************************************************************************/
    /********************************管理员***************************************/
    /*****************************************************************************/

    @RequiresPermissions("admin")
    @ResponseBody
    @RequestMapping(value = "/deleteByAdmin", method = RequestMethod.POST)
    public JsonModel deleteByAdmin(String bookId, String reason){
        return bookService.deleteByAdmin(bookId, reason);
    }

}
